﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ShoppingListEntities;
using System.Linq;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
namespace ShoppingList.Helpers
{

   
    public static class ShopListHelpers
    {

         // private static readonly DateTime ShopToday = new DateTime(2011,12,11);
        private static readonly DateTime ShopToday = DateTime.Now;
        private static Dictionary<string, List<Product>> DictionaryProducts;

        private static Color lightThemeBackground = Color.FromArgb(255, 255, 255, 255);
        private static Color darkThemeBackground = Color.FromArgb(255, 0, 0, 0); 

        public  struct ProductMenuCommands
        {

           public string ProductId;
           public string CommandName;

        }

        public static ImageBrush DisplayState()
        {
            SolidColorBrush backgroundBrush = Application.Current.Resources["PhoneBackgroundBrush"] as SolidColorBrush;

            if (backgroundBrush.Color == Helpers.ShopListHelpers.lightThemeBackground)
            {
                BitmapImage bitmapImage = new BitmapImage(new Uri("Images/Theme/PanoramaBackgroundLight.png", UriKind.Relative));
                ImageBrush imageBrush = new ImageBrush(); 
                imageBrush.ImageSource = bitmapImage;
                return imageBrush;
            }
            else
            {
                BitmapImage bitmapImage = new BitmapImage(new Uri("Images/Theme/PanoramaBackgroundDark.png", UriKind.Relative));
                ImageBrush imageBrush = new ImageBrush();
                imageBrush.ImageSource = bitmapImage;
                return imageBrush;
            }
        } 

        //public string ConvertDayToSpanish(DayOfWeek dayofweek)
        //{
        //    if (dayofweek == DayOfWeek.Monday) {return "Lunes"; }
        //    if (dayofweek == DayOfWeek.Tuesday) { return "Martes"; }
        //    if (dayofweek == DayOfWeek.Wednesday) { return "Miercoles"; }
        //    if (dayofweek == DayOfWeek.Thursday) { return "Jueves"; }
        //    if (dayofweek == DayOfWeek.Friday) { return "Viernes"; }
        //    return string.Empty;
        //}

        public static void VerifyExistShopList()
        {
            using (ShoppingListDataContext dataContext = new ShoppingListDataContext(DataBaseHelpers.Cnn))
            {
                //        dataContext.DeleteDatabase();
                if (dataContext.DatabaseExists())
                {
                    var query = dataContext.ShoppingLists
                        .Where(x => x.ShoppingDate == ShopToday.ToShortDateString()).SingleOrDefault();
                    if (query == null)
                    {
                        ShoppingListEntities.ShoppingList newlist = new ShoppingListEntities.ShoppingList
                        {
                        ShoppingListId= Guid.NewGuid(),
                        ShoppingDate= ShopToday.ToShortDateString()
                        };

                        dataContext.ShoppingLists.InsertOnSubmit(newlist);

                        dataContext.SubmitChanges();
                    }

                }
            }
        }

     

        public static void CloseShopLists()
        {

        }





    

        public static ShoppingListEntities.ShoppingList getShoppingList()
        {
            using (ShoppingListDataContext dataContext = new ShoppingListDataContext(DataBaseHelpers.Cnn))
            {
                //        dataContext.DeleteDatabase();
                if (dataContext.DatabaseExists())
                {
                    VerifyExistShopList();
                    var query = dataContext.ShoppingLists
                        .Where(x => x.ShoppingDate == ShopToday.ToShortDateString()).SingleOrDefault();
                    if (query == null)
                    {
                        
                        getShoppingList();
                    }
                    else
                    {
                        return query;
                    }

                }
            }
            return null;
        }

        public static Dictionary<string, List<Product>> getJumpListProducts()
        {
            GroupingProducts();
            if (DictionaryProducts != null)
            {
                return DictionaryProducts;
            }
            return null;
        }


        public static Dictionary<string, List<Product>> getAviableJumpListProducts()
        {
            GroupingProducts();
            if (DictionaryProducts != null)
            {
                using (ShoppingListDataContext dataContext = new ShoppingListDataContext(DataBaseHelpers.Cnn))
                {
                    //        dataContext.DeleteDatabase();
                    if (dataContext.DatabaseExists())
                    {
                        ShoppingListEntities.ShoppingList shoppinglist = getShoppingList();
                      

                        List<Product> listaviableproducts = new List<Product>();
                        var products = dataContext.ShoppingListProducts
                            .Where(x => x.ShoppingListId == shoppinglist.ShoppingListId)
                            .Select(y=> y.Product);

                        foreach (var item in DictionaryProducts)
                        {
                            foreach (var subitem in item.Value)
                            {
                                if (!products.Contains(subitem))
                                {
                                    listaviableproducts.Add(subitem);
                                }
                            }
                        }

                        return GroupingAviableProducts(listaviableproducts);
                    }
                }

              

               

               
            }
            return null;
        }

        private static void GroupingProducts()
        {
            using (ShoppingListDataContext dataContext = new ShoppingListDataContext(DataBaseHelpers.Cnn))
            {
                //        dataContext.DeleteDatabase();
                if (dataContext.DatabaseExists())
                {



                    DictionaryProducts = new Dictionary<string, List<Product>>();
                    var resultA = dataContext.Products.Where(x => x.Name[0] == 'A' || x.Name[0] == 'a');
                    var resultB = dataContext.Products.Where(x => x.Name[0] == 'B' || x.Name[0] == 'b');
                    var resultC = dataContext.Products.Where(x => x.Name[0] == 'C' || x.Name[0] == 'c');
                    var resultD = dataContext.Products.Where(x => x.Name[0] == 'D' || x.Name[0] == 'd');
                    var resultE = dataContext.Products.Where(x => x.Name[0] == 'E' || x.Name[0] == 'e');
                    var resultF = dataContext.Products.Where(x => x.Name[0] == 'F' || x.Name[0] == 'f');
                    var resultG = dataContext.Products.Where(x => x.Name[0] == 'G' || x.Name[0] == 'g');
                    var resultH = dataContext.Products.Where(x => x.Name[0] == 'H' || x.Name[0] == 'h');
                    var resultI = dataContext.Products.Where(x => x.Name[0] == 'I' || x.Name[0] == 'i');
                    var resultJ = dataContext.Products.Where(x => x.Name[0] == 'J' || x.Name[0] == 'j');
                    var resultK = dataContext.Products.Where(x => x.Name[0] == 'K' || x.Name[0] == 'k');
                    var resultL = dataContext.Products.Where(x => x.Name[0] == 'L' || x.Name[0] == 'l');
                    var resultM = dataContext.Products.Where(x => x.Name[0] == 'M' || x.Name[0] == 'm');
                    var resultN = dataContext.Products.Where(x => x.Name[0] == 'N' || x.Name[0] == 'n');
                    var resultO = dataContext.Products.Where(x => x.Name[0] == 'O' || x.Name[0] == 'o');
                    var resultP = dataContext.Products.Where(x => x.Name[0] == 'P' || x.Name[0] == 'p');
                    var resultQ = dataContext.Products.Where(x => x.Name[0] == 'Q' || x.Name[0] == 'q');
                    var resultR = dataContext.Products.Where(x => x.Name[0] == 'R' || x.Name[0] == 'r');
                    var resultS = dataContext.Products.Where(x => x.Name[0] == 'S' || x.Name[0] == 's');
                    var resultT = dataContext.Products.Where(x => x.Name[0] == 'T' || x.Name[0] == 't');
                    var resultU = dataContext.Products.Where(x => x.Name[0] == 'U' || x.Name[0] == 'u');
                    var resultV = dataContext.Products.Where(x => x.Name[0] == 'V' || x.Name[0] == 'v');
                    var resultW = dataContext.Products.Where(x => x.Name[0] == 'W' || x.Name[0] == 'w');
                    var resultX = dataContext.Products.Where(x => x.Name[0] == 'X' || x.Name[0] == 'x');
                    var resultY = dataContext.Products.Where(x => x.Name[0] == 'Y' || x.Name[0] == 'y');
                    var resultZ = dataContext.Products.Where(x => x.Name[0] == 'Z' || x.Name[0] == 'z');
                    DictionaryProducts.Add("a", resultA.ToList());
                    DictionaryProducts.Add("b", resultB.ToList());
                    DictionaryProducts.Add("c", resultC.ToList());
                    DictionaryProducts.Add("d", resultD.ToList());
                    DictionaryProducts.Add("e", resultE.ToList());
                    DictionaryProducts.Add("f", resultF.ToList());
                    DictionaryProducts.Add("g", resultG.ToList());
                    DictionaryProducts.Add("h", resultH.ToList());
                    DictionaryProducts.Add("i", resultI.ToList());
                    DictionaryProducts.Add("j", resultJ.ToList());
                    DictionaryProducts.Add("k", resultK.ToList());
                    DictionaryProducts.Add("l", resultL.ToList());
                    DictionaryProducts.Add("m", resultM.ToList());
                    DictionaryProducts.Add("n", resultN.ToList());
                    DictionaryProducts.Add("o", resultO.ToList());
                    DictionaryProducts.Add("p", resultP.ToList());
                    DictionaryProducts.Add("q", resultQ.ToList());
                    DictionaryProducts.Add("r", resultR.ToList());
                    DictionaryProducts.Add("s", resultS.ToList());
                    DictionaryProducts.Add("t", resultT.ToList());
                    DictionaryProducts.Add("u", resultU.ToList());
                    DictionaryProducts.Add("v", resultV.ToList());
                    DictionaryProducts.Add("w", resultW.ToList());
                    DictionaryProducts.Add("x", resultX.ToList());
                    DictionaryProducts.Add("y", resultY.ToList());
                    DictionaryProducts.Add("z", resultZ.ToList());
                }
            }
        }

        private static Dictionary<string, List<Product>> GroupingAviableProducts( List<Product> Products)
        {
           

                Dictionary<string, List<Product>> dictionary = new Dictionary<string, List<Product>>();
                //        dataContext.DeleteDatabase();
                                 
                    var resultA = Products.Where(x => x.Name[0] == 'A' || x.Name[0] == 'a');
                    var resultB = Products.Where(x => x.Name[0] == 'B' || x.Name[0] == 'b');
                    var resultC = Products.Where(x => x.Name[0] == 'C' || x.Name[0] == 'c');
                    var resultD = Products.Where(x => x.Name[0] == 'D' || x.Name[0] == 'd');
                    var resultE = Products.Where(x => x.Name[0] == 'E' || x.Name[0] == 'e');
                    var resultF = Products.Where(x => x.Name[0] == 'F' || x.Name[0] == 'f');
                    var resultG = Products.Where(x => x.Name[0] == 'G' || x.Name[0] == 'g');
                    var resultH = Products.Where(x => x.Name[0] == 'H' || x.Name[0] == 'h');
                    var resultI = Products.Where(x => x.Name[0] == 'I' || x.Name[0] == 'i');
                    var resultJ = Products.Where(x => x.Name[0] == 'J' || x.Name[0] == 'j');
                    var resultK = Products.Where(x => x.Name[0] == 'K' || x.Name[0] == 'k');
                    var resultL = Products.Where(x => x.Name[0] == 'L' || x.Name[0] == 'l');
                    var resultM = Products.Where(x => x.Name[0] == 'M' || x.Name[0] == 'm');
                    var resultN = Products.Where(x => x.Name[0] == 'N' || x.Name[0] == 'n');
                    var resultO = Products.Where(x => x.Name[0] == 'O' || x.Name[0] == 'o');
                    var resultP = Products.Where(x => x.Name[0] == 'P' || x.Name[0] == 'p');
                    var resultQ = Products.Where(x => x.Name[0] == 'Q' || x.Name[0] == 'q');
                    var resultR = Products.Where(x => x.Name[0] == 'R' || x.Name[0] == 'r');
                    var resultS = Products.Where(x => x.Name[0] == 'S' || x.Name[0] == 's');
                    var resultT = Products.Where(x => x.Name[0] == 'T' || x.Name[0] == 't');
                    var resultU = Products.Where(x => x.Name[0] == 'U' || x.Name[0] == 'u');
                    var resultV = Products.Where(x => x.Name[0] == 'V' || x.Name[0] == 'v');
                    var resultW = Products.Where(x => x.Name[0] == 'W' || x.Name[0] == 'w');
                    var resultX = Products.Where(x => x.Name[0] == 'X' || x.Name[0] == 'x');
                    var resultY = Products.Where(x => x.Name[0] == 'Y' || x.Name[0] == 'y');
                    var resultZ = Products.Where(x => x.Name[0] == 'Z' || x.Name[0] == 'z');
                    dictionary.Add("a", resultA.ToList());
                    dictionary.Add("b", resultB.ToList());
                    dictionary.Add("c", resultC.ToList());
                    dictionary.Add("d", resultD.ToList());
                    dictionary.Add("e", resultE.ToList());
                    dictionary.Add("f", resultF.ToList());
                    dictionary.Add("g", resultG.ToList());
                    dictionary.Add("h", resultH.ToList());
                    dictionary.Add("i", resultI.ToList());
                    dictionary.Add("j", resultJ.ToList());
                    dictionary.Add("k", resultK.ToList());
                    dictionary.Add("l", resultL.ToList());
                    dictionary.Add("m", resultM.ToList());
                    dictionary.Add("n", resultN.ToList());
                    dictionary.Add("o", resultO.ToList());
                    dictionary.Add("p", resultP.ToList());
                    dictionary.Add("q", resultQ.ToList());
                    dictionary.Add("r", resultR.ToList());
                    dictionary.Add("s", resultS.ToList());
                    dictionary.Add("t", resultT.ToList());
                    dictionary.Add("u", resultU.ToList());
                    dictionary.Add("v", resultV.ToList());
                    dictionary.Add("w", resultW.ToList());
                    dictionary.Add("x", resultX.ToList());
                    dictionary.Add("y", resultY.ToList());
                    dictionary.Add("z", resultZ.ToList());

                    return dictionary;
                
            
        }


    }
}
